<template>
  <a-modal
    title="创建新代理"
    :visible="visible"
    @ok="onSubmit"
    @cancel="visible = false"
  >
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="{ span: 8 }"
      :wrapper-col="{ span: 14 }"
    >
      <a-form-model-item label="用户名" prop="username">
        <a-input v-model="form.username" />
      </a-form-model-item>

      <a-form-model-item label="密码" prop="username">
        <a-input v-model="form.password" />
      </a-form-model-item>

      <a-form-model-item ref="fee_ratio" label="买币佣金比" prop="fee_ratio">
        <a-input-number
          v-model="form.fee_ratio"
          :max="maxFeeRatio"
          :min="0"
          :step="0.001"
        />
      </a-form-model-item>

      <a-form-model-item label="单笔奖励" prop="tip">
        <a-input-number v-model="form.tip" :max="maxTip" :min="0" />
      </a-form-model-item>

      <a-form-model-item label="下级买币解锁时间" prop="freeze_minutes">
        <a-input-number
          v-model="form.freeze_minutes"
          :min="1"
          :step="1"
          :precision="0"
        />
      </a-form-model-item>

      <a-form-model-item label="手机号" prop="mobile">
        <a-input v-model="form.mobile" />
      </a-form-model-item>

      <a-form-model-item label="备注" prop="remark">
        <a-input v-model="form.remark" />
      </a-form-model-item>

      <a-form-model-item label="状态">
        <a-radio-group v-model="form.enable">
          <a-radio :value="true"> 启用 </a-radio>
          <a-radio :value="false"> 禁用 </a-radio>
        </a-radio-group>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      visible: false,
      maxFeeRatio: 0,
      maxTip: 0,
      form: {
        username: '',
        fee_ratio: 0,
        tip: 0,
        freeze_minutes: 1,
        mobile: '',
        remark: '',
        password: '',
        enable: true,
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
          // {
          //   min: 6,
          //   message: '请输入6个字符以上',
          //   trigger: 'blur',
          // },
        ],
        password: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
        fee_ratio: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
        tip: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
          freeze_minutes: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  computed: {
    ...mapGetters('account', ['sys']),
  },
  methods: {
    show() {
      //   console.log(this.sys)
      this.maxFeeRatio = parseFloat(this.sys.global_fee_ratio)
      this.maxTip = parseInt(this.sys.global_tip)
      this.visible = true
    },
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // console.log(this.form)
          this.axios.post('/a/agents', this.form).then((res) => {
            this.visible = false
            this.form = this.$options.data().form
            this.$message.success('创建成功')
            this.$emit('refresh')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
